package com.sgb.kjwl.viewmodel.demand;


import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.NeedsEntity;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.demand.DemandManageActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class DemandManageModel3 extends BaseViewModel {
    private DemandManageActivity activity;
    private HomeRepertory repertory;
    private Map map;
    private int pageNum;
    private int pageSize = 5;
    private int total;
    private String areaCode;

    public DemandManageModel3(DemandManageActivity activity, HomeRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    //卖家推荐需求
    public void getNeedsList(String cityNo, String areaNo, int type) {
        if (map == null) {
            map = new HashMap();
        }
        pageNum = 1;
        if (type == 1) {
            areaCode = repertory.getAreaCode();
        } else {
            areaCode = "";
        }
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("areaCode", areaCode);
        map.put("cityNo", cityNo);
        map.put("districtNo", areaNo);
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        if (type == 1) {
            APIManager.getInstance().getCommonAPI().getHomeNeedsList(map).enqueue(new Callback<BaseEntity<ListResponse<List<NeedsEntity>>>>() {
                @Override
                public void onResponse(Call<BaseEntity<ListResponse<List<NeedsEntity>>>> call, Response<BaseEntity<ListResponse<List<NeedsEntity>>>> response) {
                    DialogHelper.dismissProgressDialog();
                    if (response.body() == null)
                        return;
                    if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                        if (response.body().getData() == null)
                            return;
                        if (response.body().getData().getList() == null)
                            return;
                        total = response.body().getData().getTotal();
                        if (total > 0) {
                            activity.refresh(response.body().getData().getList(), response.body().getData().isLastPage());
                        } else {
                            activity.empty();
                        }
                    } else {
                        MToast.showToast(activity, response.message());
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity<ListResponse<List<NeedsEntity>>>> call, Throwable t) {
                    DialogHelper.dismissProgressDialog();
                    MToast.showToast(activity, t.getMessage());
                }
            });
        } else {
            APIManager.getInstance().getCommonAPI().getNeedsList(map).enqueue(new Callback<BaseEntity<ListResponse<List<NeedsEntity>>>>() {
                @Override
                public void onResponse(Call<BaseEntity<ListResponse<List<NeedsEntity>>>> call, Response<BaseEntity<ListResponse<List<NeedsEntity>>>> response) {
                    DialogHelper.dismissProgressDialog();
                    if (response.body() == null)
                        return;
                    if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                        if (response.body().getData() == null)
                            return;
                        if (response.body().getData().getList() == null)
                            return;
                        total = response.body().getData().getTotal();
                        if (total > 0) {
                            activity.refresh(response.body().getData().getList(), response.body().getData().isLastPage());
                        } else {
                            activity.empty();
                        }
                    } else {
                        MToast.showToast(activity, response.message());
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity<ListResponse<List<NeedsEntity>>>> call, Throwable t) {
                    DialogHelper.dismissProgressDialog();
                    MToast.showToast(activity, t.getMessage());
                }
            });
        }

    }

    //更多
    public void getMoreList(String cityNo, String areaNo, int type) {
        if (map == null) {
            map = new HashMap();
        }
        if (type == 1) {
            areaCode = repertory.getAreaCode();
        } else {
            areaCode = "";
        }
//        pageNum = pageNum++;
        map.put("pageNum", pageNum++);
        map.put("pageSize", pageSize);
        map.put("areaCode", areaCode);
        map.put("cityNo", cityNo);
        map.put("districtNo", areaNo);
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        if (type == 1) {
            APIManager.getInstance().getCommonAPI().getHomeNeedsList(map).enqueue(new Callback<BaseEntity<ListResponse<List<NeedsEntity>>>>() {
                @Override
                public void onResponse(Call<BaseEntity<ListResponse<List<NeedsEntity>>>> call, Response<BaseEntity<ListResponse<List<NeedsEntity>>>> response) {
                    DialogHelper.dismissProgressDialog();
                    if (response.body() == null)
                        return;
                    if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                        if (response.body().getData() == null)
                            return;
                        if (response.body().getData().getList() == null)
                            return;
                        activity.loadMore(response.body().getData().getList(), response.body().getData().isLastPage());
                    } else {
                        MToast.showToast(activity, response.message());
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity<ListResponse<List<NeedsEntity>>>> call, Throwable t) {
                    DialogHelper.dismissProgressDialog();
                    MToast.showToast(activity, t.getMessage());
                }
            });
        } else {
            APIManager.getInstance().getCommonAPI().getNeedsList(map).enqueue(new Callback<BaseEntity<ListResponse<List<NeedsEntity>>>>() {
                @Override
                public void onResponse(Call<BaseEntity<ListResponse<List<NeedsEntity>>>> call, Response<BaseEntity<ListResponse<List<NeedsEntity>>>> response) {
                    DialogHelper.dismissProgressDialog();
                    if (response.body() == null)
                        return;
                    if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                        if (response.body().getData() == null)
                            return;
                        if (response.body().getData().getList() == null)
                            return;
                        activity.loadMore(response.body().getData().getList(), response.body().getData().isLastPage());
                    } else {
                        MToast.showToast(activity, response.message());
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity<ListResponse<List<NeedsEntity>>>> call, Throwable t) {
                    DialogHelper.dismissProgressDialog();
                    MToast.showToast(activity, t.getMessage());
                }
            });
        }
    }

    //获取城市地区
    public void getDeviceAreaModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(true);
        //暂时写死，新疆地区能查询出城市
        /*repertory.getDeviceArea(Integer.parseInt(repertory.getAreaCode()))
                .subscribe(new Consumer<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>() {
                    @Override
                    public void accept(BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>> listBaseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        fragment.showAreaPop(listBaseEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                    }
                });*/

        APIManager.getInstance().getDeviceAPI().getAreaSecondAndThird(repertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> call, Response<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showCityPop(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
